package com.xique.door.controller;


import com.xique.common.core.config.Global;
import com.xique.common.core.utils.poi.ExcelUtil;
import com.xique.common.core.web.controller.BaseController;
import com.xique.common.core.web.domain.AjaxResult;
import com.xique.common.core.web.page.TableDataInfo;
import com.xique.common.log.annotation.Log;
import com.xique.common.log.enums.BusinessType;
import com.xique.common.security.annotation.PreAuthorize;
import com.xique.door.bean.domain.PermissionGroup;
import com.xique.door.bean.domain.Visitor;
import com.xique.door.bean.request.commandV1.DeviceCommandDownloadRequest;
import com.xique.door.bean.request.permissionGroup.PermissionGroupAddRequest;
import com.xique.door.bean.request.permissionGroup.PermissionGroupDeleteRequest;
import com.xique.door.biz.service.IPermissionGroupBizService;
import com.xique.door.biz.service.IDeviceCommandService;
import com.xique.door.service.IPermissionGroupService;
import com.xique.door.service.IVisitorService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

/**
 * 权限组Controller
 *
 * @author ww
 * @date 2021-06-11
 */
@Deprecated
@Controller
@RequestMapping("/oms/permissionGroup")
public class PermissionGroupController extends BaseController
{
    private String prefix = "oms/permissionGroup";

    @Autowired
    private IPermissionGroupService permissionGroupService;

    @Autowired
    private IDeviceCommandService deviceCommandService;

    @Autowired
    private IPermissionGroupBizService permissionGroupBizService;

    @Autowired
    private IVisitorService visitorService;

     @PreAuthorize(hasPermi="oms:permissionGroup:view")
    @GetMapping("/{parkId}")
    public String permissionGroup(@PathVariable("parkId") Long parkId, ModelMap mmap)
    {
        mmap.put("parkId", parkId);
        mmap.put("apiUrl", Global.getApiUrl());
        return prefix + "/permissionGroup";
    }

    /**
     * 查询权限组列表
     */
     @PreAuthorize(hasPermi="oms:permissionGroup:list")
    @PostMapping("/list/{parkId}")
    @ResponseBody
    public TableDataInfo list(PermissionGroup permissionGroup)
    {
        startPage();
        List<PermissionGroup> list = permissionGroupService.selectPermissionGroupList(permissionGroup);
        return getDataTable(list);
    }

    /**
     * 导出权限组列表
     */
     @PreAuthorize(hasPermi="oms:permissionGroup:export")
    @PostMapping("/export")
    @ResponseBody
    public void export(HttpServletResponse response, PermissionGroup permissionGroup) throws IOException {
        List<PermissionGroup> list = permissionGroupService.selectPermissionGroupList(permissionGroup);
        ExcelUtil<PermissionGroup> util = new ExcelUtil<PermissionGroup>(PermissionGroup.class);
         util.exportExcel(response,list, "permissionGroup");
    }

    /**
     * 新增权限组
     */
    @GetMapping("/add/{parkId}")
    public String add(@PathVariable("parkId") Long parkId, ModelMap mmap)
    {
        return prefix + "/add";
    }

    /**
     * 新增保存权限组
     */
     @PreAuthorize(hasPermi="oms:permissionGroup:add")
    @Log(title = "权限组", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(PermissionGroup permissionGroup)
    {
        return toAjax(permissionGroupService.insertPermissionGroup(permissionGroup));
    }

    /**
     * 新增保存权限组
     */
     @PreAuthorize(hasPermi="oms:permissionGroup:add")
    @Log(title = "权限组", businessType = BusinessType.INSERT)
    @PostMapping("/add/v2")
    @ResponseBody
    public AjaxResult addSaveV2(PermissionGroupAddRequest request)
    {
        return permissionGroupBizService.addGroup(request);
    }

    /**
     * 修改权限组
     */
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") Long id, ModelMap mmap)
    {
        PermissionGroup permissionGroup = permissionGroupService.selectPermissionGroupById(id);
        mmap.put("permissionGroup", permissionGroup);
        return prefix + "/edit";
    }

    /**
     * 修改保存权限组
     */
     @PreAuthorize(hasPermi="oms:permissionGroup:edit")
    @Log(title = "权限组", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(PermissionGroup permissionGroup)
    {
        return toAjax(permissionGroupService.updatePermissionGroup(permissionGroup));
    }

    /**
     * 删除权限组
     */
     @PreAuthorize(hasPermi="oms:permissionGroup:remove")
    @Log(title = "权限组", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(PermissionGroupDeleteRequest request)
    {
        return permissionGroupBizService.deleteGroup(request);
    }

    /**
     * 同步单用户至设备
     * @return
     */
    @Log(title = "同步单用户至设备", businessType = BusinessType.COMMAND)
    @PostMapping("/downSingleUser")
    @ResponseBody
    public AjaxResult downSingleUser(DeviceCommandDownloadRequest request){
        return deviceCommandService.downloadUserInfo(request);
    }

    /**
     * 同步单卡片至设备
     * @return
     */
    @Log(title = "同步单卡片至设备", businessType = BusinessType.COMMAND)
    @PostMapping("/downSingleDCard")
    @ResponseBody
    public AjaxResult downSingleDCard(DeviceCommandDownloadRequest request){
        return deviceCommandService.downloadDCardInfo(request);
    }

    /**
     * 同步所有用户至设备
     * @param request
     * @return
     */
    @Log(title = "同步所有用户至设备", businessType = BusinessType.COMMAND)
    @PostMapping("/downAllUsers")
    @ResponseBody
    public AjaxResult downAllUsers(DeviceCommandDownloadRequest request){
        return deviceCommandService.downloadAllUserInfo(request);
    }

    /**
     * 同步单用户至设备
     * @return
     */
    @Log(title = "同步访客信息至设备", businessType = BusinessType.COMMAND)
    @PostMapping("/downSingleVisitor")
    @ResponseBody
    public AjaxResult downSingleVisitor(DeviceCommandDownloadRequest request){
        Visitor visitor = visitorService.selectVisitorById(request.getVisitorId());
        request.setUserId(visitor.getUserId());
        request.setVisitor(visitor);
        return deviceCommandService.downloadVisitorInfo(request);
    }

    /**
     * 设备状态修改
     */
    @Log(title = "权限组", businessType = BusinessType.UPDATE)
     @PreAuthorize(hasPermi="oms:permissionGroup:edit")
    @PostMapping("/changeStatus")
    @ResponseBody
    public AjaxResult changeStatus(Long id, String status)
    {
        return permissionGroupBizService.changeStatus(id, status);
    }
}
